package org.hhgzs.modules.erp.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.entity.Account;
import org.hhgzs.modules.erp.entity.MaterialCategory;
import org.hhgzs.modules.erp.service.AccountService;
import org.hhgzs.modules.erp.service.MaterialCategoryService;
import org.hhgzs.modules.erp.vo.TreeNode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 产品类型表 前端控制器
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/materialCategory")
@Slf4j
@RequiredArgsConstructor
@Api("结算账户管理")
public class MaterialCategoryController extends JeecgController<MaterialCategory, MaterialCategoryService> {

    private final MaterialCategoryService materialCategoryService;

    /**
     * create by: cjl
     * description:
     * 获取商品类别树数据
     * create time: 2019/2/19 11:49
     *
     * @return com.alibaba.fastjson.JSONArray
     * @Param:
     */
    @RequestMapping(value = "/getMaterialCategoryTree")
    public JSONArray getMaterialCategoryTree(@RequestParam("id") Long id) throws Exception {
        JSONArray arr = new JSONArray();
        List<TreeNode> materialCategoryTree = materialCategoryService.getMaterialCategoryTree(id);
        if (materialCategoryTree != null && materialCategoryTree.size() > 0) {
            for (TreeNode node : materialCategoryTree) {
                String str = JSON.toJSONString(node);
                JSONObject obj = JSON.parseObject(str);
                arr.add(obj);
            }
        }
        return arr;
    }

    /**
     * 根据id来查询商品名称
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping(value = "/findById")
    public Result findById(@RequestParam("id") Long id, HttpServletRequest request) throws Exception {
        try {
            QueryWrapper<MaterialCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(MaterialCategory::getId, id);
            List<MaterialCategory> dataList = materialCategoryService.list(queryWrapper);
            JSONObject outer = new JSONObject();
            if (ObjectUtil.isNotEmpty(dataList)) {
                for (MaterialCategory mc : dataList) {
                    outer.put("id", mc.getId());
                    outer.put("name", mc.getName());
                    outer.put("parentId", mc.getParentId());
                    queryWrapper.clear();
                    queryWrapper.lambda().eq(MaterialCategory::getParentId, id);
                    List<MaterialCategory> dataParentList = materialCategoryService.list(queryWrapper);
                    if (dataParentList != null && dataParentList.size() > 0) {
                        outer.put("parentName", dataParentList.get(0).getName());
                    }
                    outer.put("sort", mc.getSort());
                    outer.put("serialNo", mc.getSerialNo());
                    outer.put("remark", mc.getRemark());
                }
            }
            return Result.OK(outer);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取数据失败");
        }
    }
}
